#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <unistd.h>
#include <cstring>
#include <pthread.h>
#include <cstdint>

namespace ThreadModlue
{

    static uint32_t number = 1;

    template<class T>
    class Thread
    {
        using func_t = std::function<void(T)>; 

    private:
        void EnableRunning()
        {
            _isrunning = true;
        }

        void EnableDetach()
        {
            _isdetach = true;
            std::cout << "线程已经分离" << std::endl;
        }

        static void* Routine(void* args)
        {
            Thread<T>* self = static_cast<Thread<T>*>(args);
            EnableRunning();
            if(self->_isdetach)
                //Detach();
            self->_func;
            return nullptr;
        }

    public:
        Thread(func_t func, T data)
            :_tid(0)
            ,_isrunning(false)
            ,_isdetach(false)
            ,_func(func)
            ,_res(nullptr)
            ,_data(data)
        {
            _name = "thread-" + std::to_string(number++);
        }

        bool Start()
        {
            int n = pthread_create(&_tid, nullptr, Routine, this);
            if(n != 0)
            {
                std::cerr << "create error: " << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << "create success!" << std::endl;
                return true;
            }
        }

        void Detach()
        {
            if(!_isrunning)
                return;
            if(_isdetach)
                return;
            int n = pthread_detach(_tid);
            if(n != 0)
            {
                std::cerr << "detach error: " << strerror(n) << std::endl;
            }
            else
            {
                std::cout << "detach success" << std::endl;
            }
        }

        bool Stop()
        {
            if(_isrunning)
            {
                int n = pthread_cancel(_tid);
                if(n != 0)
                {
                    std::cerr << "stop error" << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    std::cout << "cancel success" << std::endl;
                    _isrunning = false;
                    return true;
                }
            }
            std::cout << "stop error" << std::endl;
            return false;
        }

        bool Join()
        {
            if(_isrunning)
            {
                if(_isdetach)
                {
                    std::cout << "线程已经被分离" << std::endl;
                    return false;
                }
                int n = pthread_join(_tid, &res);
                if(n != 0)
                {
                    std::cerr << "join error: " << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    std::cout << "join success" << std::endl;
                    return true;
                }
            }

            std::cout << "无线程运行，不需要join" << std::endl;
            return false;
        }

        ~Thread()
        {}
    private:
        pthread_t _tid;
        bool _isrunning;
        bool _isdetach;
        func_t _func;
        void* _res;
        std::string _name;
        T _data;
    };
}